package com.ylbh.modules.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.pojo.activity.dto.ActivityGoodsDTO;
import com.ylbh.common.pojo.activity.vo.ShoppingActivityGoodsVO;
import com.ylbh.common.pojo.activity.vo.WholePointActivitiesTimeVO;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.common.utils.Query;
import com.ylbh.common.utils.RedisUtils;
import com.ylbh.modules.activity.mapper.SpikeGoodsMapper;
import com.ylbh.modules.activity.service.SpikeGoodsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("spikeGoodsService")
public class SpikeGoodsServiceImpl extends ServiceImpl<SpikeGoodsMapper, ActivityGoodsDTO> implements SpikeGoodsService {
    @Autowired
    SpikeGoodsMapper spikeGoodsMapper;
    @Autowired
    RedisUtils redisUtils;

    /**
     * 获取活动商品列表
     * @param params
     * @return
     */
    @Override
    public PageUtils queryActivityGoodsList(Map<String, Object> params) {
        QueryWrapper<ActivityGoodsDTO> activityGoodsDTOQueryWrapper = new QueryWrapper<>();

        String key = params.get("key").toString();
        if(StringUtils.isNotBlank(key)){
            activityGoodsDTOQueryWrapper.eq("activity_id",key);
        }
        String goodsName = params.get("goodsName").toString();
        if(StringUtils.isNotBlank(goodsName)) {
            activityGoodsDTOQueryWrapper.like("goods_name",goodsName);
        }

        activityGoodsDTOQueryWrapper.eq("delete_status" ,1);
        activityGoodsDTOQueryWrapper.orderByAsc("goods_sequence");
        activityGoodsDTOQueryWrapper.orderByDesc("create_time");
        IPage<ActivityGoodsDTO> page = this.page(
                new Query<ActivityGoodsDTO>().getPage(params),
                activityGoodsDTOQueryWrapper
        );
        if (null != page.getRecords() && page.getRecords().size() > 0) {
            for (int i = 0; i < page.getRecords().size(); i++) {
                // 查询商品的赠送比例
                String giveIntegralType = spikeGoodsMapper.selectGiveIntegralTypeByGoodsId(page.getRecords().get(i).getGoodsId());
                page.getRecords().get(i).setGoodsDiscount(giveIntegralType);
            }
        }
        return new PageUtils(page);
    }

    /**
     * 根据活动id查找排序序号
     * @param activityId
     * @return
     */
    @Override
    public Integer getLastSequence(String activityId) {
        Integer sequence = spikeGoodsMapper.getLastSequence(activityId);
        return sequence==null ? 0 :sequence;
    }

    /**
     * 通过活动id修改活动商品
     * @param activityGoodsDTO
     */
    @Override
    public void updateByActivityId(ActivityGoodsDTO activityGoodsDTO) {
        redisUtils.deleteKeys("querySeckillGoodsInfoByPage*");
        redisUtils.deleteKeys("com.yilianbaihui.shopping.seckill*");
        spikeGoodsMapper.updateByActivityId(activityGoodsDTO);
    }

    /**
     * 修改商品上下架状态
     * @param activityGoodsDTO
     */
    @Override
    public void updateStateByActivityId(ActivityGoodsDTO activityGoodsDTO) {
        redisUtils.deleteKeys("querySeckillGoodsInfoByPage*");
        redisUtils.deleteKeys("com.yilianbaihui.shopping.seckill*");
        QueryWrapper<ActivityGoodsDTO> activityGoodsDTOQueryWrapper = new QueryWrapper<>();
        activityGoodsDTOQueryWrapper.eq("activity_id",activityGoodsDTO.getActivityId());
        this.update(activityGoodsDTO,activityGoodsDTOQueryWrapper);
    }

    /**
     * 查询出整点分场的开始时间和结束时间
     * @param activityId
     * @return
     */
    @Override
    public WholePointActivitiesTimeVO getActivityTime(String activityId) {
        return spikeGoodsMapper.selectActivityTime(activityId);
    }

    /**
     * 复制数据
     * @param shoppingActivityGoods
     * @return
     */
    @Override
    public ShoppingActivityGoodsVO copyData(ActivityGoodsDTO shoppingActivityGoods) {
        ShoppingActivityGoodsVO vo = new ShoppingActivityGoodsVO();
        vo.setActivityId(shoppingActivityGoods.getActivityId());
        vo.setGoodsId(shoppingActivityGoods.getGoodsId());
        vo.setVipPrice(shoppingActivityGoods.getVipPrice());
        vo.setGoodsPrice(shoppingActivityGoods.getGoodsPrice());
        vo.setGoodsIntegral(shoppingActivityGoods.getGoodsIntegral());
        vo.setPriceType(shoppingActivityGoods.getPriceType());
        vo.setSendIntegral(shoppingActivityGoods.getSendIntegral());
        if (null != shoppingActivityGoods.getGoodsSaleNum())
            vo.setGoodsSaleNum(shoppingActivityGoods.getGoodsSaleNum().toString());
        vo.setGoodsInventory(shoppingActivityGoods.getGoodsInventory());
        vo.setGoodsInventoryDetail(shoppingActivityGoods.getGoodsInventoryDetail());
        vo.setExpressTransFee(shoppingActivityGoods.getExpressTransFee());
        vo.setGoodsCostingPrice(shoppingActivityGoods.getGoodsCostingPrice());
        vo.setActivityGoodsInventoryDetail(shoppingActivityGoods.getActivityGoodsInventoryDetail());
        vo.setGoodsInputInventory(shoppingActivityGoods.getGoodsInventory());
        return vo;
    }

    /**
     * 获取开始时间到结束时间之间的秒数
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public Long addRedisInvalidTime(Date startTime, Date endTime) {
        long different = endTime.getTime() - startTime.getTime();

        long secondsInMilli = 1000;
//        long minutesInMilli = secondsInMilli * 60;
//        long hoursInMilli = minutesInMilli * 60;
//        long daysInMilli = hoursInMilli * 24;

//        long elapsedDays = different / daysInMilli;
//        different = different % daysInMilli;
//
//        long elapsedHours = different / hoursInMilli;
//        different = different % hoursInMilli;
//
//        long elapsedMinutes = different / minutesInMilli;
//        different = different % minutesInMilli;

        long elapsedSeconds = different / secondsInMilli;

        return elapsedSeconds;
    }

    @Override
    public WholePointActivitiesTimeVO getActivityTimeById(String id) {
        return spikeGoodsMapper.selectActivityTimeById(id);
    }

    @Override
    public ActivityGoodsDTO selectActivityGoods(String activityId) {
        return spikeGoodsMapper.selectActivityGoods(activityId);
    }

    /**
     * 查询活动的状态
     * @param activityId
     * @return
     */
    @Override
    public String selectActivityState(String activityId) {
        return spikeGoodsMapper.selectActivityState(activityId);
    }
}
